<template>
  <div class="app-container">
    <div class="query">
      <div>
        <el-button size="mini" type="primary" icon="el-icon-plus" circle @click=";form = {};dialogVisible = true ;editPassword = true "/>
      </div>
      <div>
        <el-button size="mini" icon="el-icon-refresh" circle @click="initList"/>
      </div>
    </div>
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column align="center" label="#" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="登陆账号" width="100">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色" width="120">
        <template slot-scope="scope">
          {{ scope.row.roleId | filterRole(roles) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" width="120">
        <template slot-scope="scope">
          {{ scope.row.realname }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号" width="130">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="邮箱" width="150">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="170">
        <template slot-scope="scope">
          {{ scope.row.created | parseTime }}
        </template>
      </el-table-column>
<!--      <el-table-column align="center" label="管理员类型" width="170">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-if="scope.row.type == 'SYSTEM'" type="success">系统管理员</el-tag>-->
<!--          <el-tag v-else type="danger">代理商</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column align="center" label="最近登陆" width="400">
        <template slot-scope="scope"> {{ scope.row.created | parseTime }}( {{ scope.row.ip }})</template>
      </el-table-column>
      <el-table-column align="center" label="管理" width="100">
        <template slot-scope="scope">
          <el-button  size="mini" type="primary" icon="el-icon-edit" circle @click=" () => { form = scope.row;dialogVisible = true }" />
          <el-button size="mini" type="danger" icon="el-icon-delete" circle/>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加/修改管理员" :visible.sync="dialogVisible" width="40%">
      <el-form label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.realname"/>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.roleId" placeholder="选择角色">
            <el-option v-for="item in roles" :key="item.id" :label="item.roleName" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="登陆账号">
          <el-input v-model="form.username"/>
        </el-form-item>
        <el-form-item label="登陆密码">
          <el-input v-model="form.password" type="password" :disabled="!editPassword">
            <el-button slot="append" icon="el-icon-edit" @click="() => (editPassword = !editPassword)"/>
          </el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.mobile"/>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="状态">
            <el-option label="正常" :value="1"/>
            <el-option label="禁用" :value="-1"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  filters: {
    filterRole(roleId, roles) {
      var roleName = ''
      roles.forEach((e) => {
        if (e.id === roleId) {
          roleName = e.roleName
          return true
        }
      })
      return roleName
    }
  },
  data() {
    return {
      form: {},
      dialogVisible: false,
      list: [],
      query: {page: 1, size: 20, total: 0, type: 'SYSTEM'},
      roles: [],
      editPassword: false
    }
  },
  created() {
    this.initRoles()
    this.initList()
  },
  methods: {
    initList() {
      this.$api.get('/api/admin/admin', this.query).then((res) => {
        this.list = res.data
        this.query.total = res.total
      })
    },
    initRoles() {
      this.$api.get('/api/admin/role', {page: 1, size: 100}).then((res) => {
        this.roles = res.data
      })
    },
    handleSubmit() {
      this.$api.put('/api/admin/admin', {editPassword: this.editPassword}, this.form).then((res) => {
        this.dialogVisible = false
        this.initList()
      })
    }
  }
}
</script>

<style></style>
